﻿package org.electric.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.codehaus.jackson.map.ObjectMapper;
import org.electric.bean.CompanyBean;
import org.electric.bean.productBean;
import org.electric.service.CompanyService;
import org.electric.util.WriterUtil;


/**
 * Servlet implementation class CompanyServlet
 */
@WebServlet("/CompanyServlet")
public class CompanyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	CompanyService cs=new CompanyService();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CompanyServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		String _method=request.getParameter("commitMethod");
//		if(_method!=null&&_method!=""){
//			if("login".equals(_method)){
//				login(request,response);
//			}else if("getproduct".equals(_method)){
//				
//				getproduct(request, response);
//			}
//		}
		String _method=request.getParameter("commitMethod");
		if(_method!=null&&!"".equals(_method)){
			if("login".equals(_method)){
				login(request,response);
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	public void getproduct(HttpServletRequest request, HttpServletResponse response){
//		response.setHeader("Content-Type","text/html;charset=utf-8" );
//		CompanyService cs=new CompanyService();
//		HttpSession session=request.getSession();
//		try {
//			//获取 产品 对象 放入list集合
//			CompanyBean companbean=(CompanyBean) session.getAttribute("companybean");
//			List<productBean> listp=cs.selectproudct(companbean.getLicense_id());
//			ObjectMapper om= WriterUtil.writeTopage();
//			om.writeValue(response.getWriter(),listp);
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
	}
	//登录操作
//	public void login(HttpServletRequest request, HttpServletResponse response){
//		HttpSession session=request.getSession();
//		//获取前端传来的用户名
//		String username=request.getParameter("username");
//		//获取前端传来的密码
//		String password=request.getParameter("password");
//		if(username!=null&&password!=null){
//			//创建companBean 对象
//			CompanyBean companbean=new CompanyBean();
//			companbean.setCompany_user(username);
//			companbean.setCompany_password(password);
//			//创建service层对象
//			CompanyService cs=new CompanyService();
//			try {
//				//拿到公司的 对象 营业执照号(唯一)
//				CompanyBean companybean= cs.login(companbean);
//				session.setAttribute("companybean", companybean);
//				if(companybean!=null){
//					request.getRequestDispatcher("second.jsp").forward(request, response);
//				}else{
//					session.setAttribute("msg", "账户或密码错误，请重新输入");
//					request.getRequestDispatcher("text.jsp").forward(request, response);
//				}
//			} catch (Exception e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		} else if(username.isEmpty()||password.isEmpty()){
//			try {
//				//提示语告知用户
//				session.setAttribute("msg", "账号或密码不能为空");
//				//转发 回到登录页面
//				request.getRequestDispatcher("text.jsp").forward(request, response);
//				
//			} catch (ServletException | IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}	
//		}
	//}
	public 	void login(HttpServletRequest request, HttpServletResponse response){
		String loginame=request.getParameter("logname");
		String logpass=request.getParameter("logpass");
		CompanyBean company=new CompanyBean();
		company.setCompany_user(loginame);
		company.setCompany_password(logpass);
	
		try {
			if(cs.selectCompany(company)!=null){
				CompanyBean com=cs.selectCompany(company);
				request.getSession().setAttribute("company", com);

				request.getRequestDispatcher("/success.jsp").forward(request, response);
			}else{
				request.setAttribute("tips", "用户名或密码有误！");
				request.getRequestDispatcher("/login.jsp").forward(request, response);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}	
}
